package com.fengcheng.service.Impl;

import com.fengcheng.service.FileService;
import com.fengcheng.util.HdfsUtil;
import com.fengcheng.util.JWTUtil;
import com.fengcheng.vo.ReturnVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class FileServiceImpl implements FileService{

    /*
     * @author 枫城
     * @createTime 2022/7/8 17:51
     * @desc token解析
     */
    public String getName(
            HttpServletRequest request
    ){
        String token = request.getHeader("token");
        return JWTUtil.getValue(token,"userName");
    }

    /*
     * @author 枫城
     * @createTime 2022/7/10 11:10
     * @desc 查看文件列表
     */
    @Override
     public List<Map<String, Object>> getFileList(
            HttpServletRequest request,
            @RequestParam("srcPath") String srcPath
    ) throws Exception {
        List<Map<String, Object>> fileList = new ArrayList<>();
        final String name = getName(request);
        srcPath = "/"+name+srcPath;
        log.info("路径信息：{}", srcPath);
        fileList = HdfsUtil.getPathInfo(srcPath);
        log.info("返回信息：{}",fileList);
        return fileList;
    }

    /*
     * @author 枫城
     * @createTime 2022/7/6 9:10
     * @desc 创建空文件夹
     */
    public ReturnVo mkdir(
            @RequestParam("path") String path,
            HttpServletRequest request){
        if (StringUtils.isEmpty(path)) {
            log.info("参数为空！");
            return new ReturnVo( "参数为空");
        }
        boolean isOk=false;
        try{
            String name =  getName(request);  // 获取到用户名
            String srcPath = "/"+name+path;
            isOk  = HdfsUtil.mkdir(srcPath);
            log.info("isOk :{}",isOk);
            log.info("当前登陆人：{}", name);
        }catch (Exception e){
            e.printStackTrace();
            log.info("==========");
        }

        if (isOk){
            return new ReturnVo( "创建成功");
        }else{
            return new ReturnVo( "创建失败");
        }
    }

    /*
     * @author 枫城
     * @createTime 2022/7/6 9:10
     * @desc 上传文件 (同一文件名，相同位置重复上传未解决)
     */
    @Override
    public ReturnVo uploadFile(MultipartFile[] fileList, String srcPath, HttpServletRequest request) throws Exception {
        if (fileList.length > 0) {
            // 确认是那个用户上传的
            final String name = getName(request);
            log.info("用户：{}", name);
            for (MultipartFile multipartFile : fileList) {
                String fileName = multipartFile.getOriginalFilename();
                String hdfsPath = "/"+name+srcPath+fileName;
                log.info("传入路径：{}", hdfsPath);
                try {
                    HdfsUtil.uploadFile(multipartFile, hdfsPath);
                }catch (Exception e){
                    return new ReturnVo("上传失败！");
                }
            }
        }
        return new ReturnVo("上传成功！");
    }

    /**
     * 下载文件（可以下载整个文件夹）
     * @param srcUri
     * @return
     */
    @Override
    public ReturnVo upDownFile(Map<String, String> srcUri, HttpServletRequest request) {
        log.info("参数：{}", srcUri.get("hdfsPath"));
        log.info("参数：{}", srcUri.get("localPath"));
        String name = getName(request);

        try {
            String srcPath =  "/"+name+srcUri.get("hdfsPath");
            log.info(srcPath);
            HdfsUtil.upDownFile(srcPath,srcUri.get("localPath"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ReturnVo("下载成功！");

    }

    @Override
    public Boolean deleteFile(String path, HttpServletRequest request) throws Exception {
        String name = getName(request);
        String srcPath = "/"+name+path;
        log.info("目标路径：{}",srcPath);
        HdfsUtil.deleteFile(srcPath);

        return true;
    }

    @Override
    public Boolean reName(String oldPath, String newName, HttpServletRequest request) throws Exception {
        String name = getName(request);
        String srcPath = "/"+name+oldPath;
        String newSrcPath = "/"+name+newName;
        log.info("旧路径：{}",srcPath);
        log.info("新路径：{}",newSrcPath);
        try {
            return HdfsUtil.reName(srcPath,newSrcPath);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 移动文件
     * @param oldPath
     * @param newPath
     * @param request
     * @return
     * @throws Exception
     */
    @Override
    public Boolean moveFile(String oldPath, String newPath, HttpServletRequest request) throws Exception {
        String name=getName(request);

        log.info("旧路径：{}",oldPath);
        log.info("新路径：{}",newPath);

        try {
            return HdfsUtil.moveFile(oldPath, newPath);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }


    }




}
